﻿/// <reference path="jquery-1.7.1-vsdoc.js" />
$(document).ready(function () {
    AssignEventHandlers();
    LoadControls();
}
    );

var TextBoxState = function (clientID, stringWaterMark, isWaterMarked, isEmpty) {
    this.ClientID = String(clientID);
    this.StringWaterMark = String(stringWaterMark);
    this.IsWaterMarked = Boolean(isWaterMarked);
    this.IsEmpty = Boolean(isEmpty);
}

var SectorState = function (ObligatoryFieldID, RightFieldID) {

    this.ObligatoryFieldID = String(ObligatoryFieldID);
    this.RightFieldID = String(RightFieldID);
    this.TextBoxArray = new Array();
    this.IsValid = new Boolean();

    this.GetTextbox = function (index) {
        return this.TextBoxArray[index];
    }
}

//Global vars initialization

var sectorID = new SectorState("");
var textboxName = new TextBoxState("", "", false, true);
var textboxSurname = new TextBoxState("", "", false, true);
var textboxCompany = new TextBoxState("", "", false, true);

var sectorContact = new SectorState("");
var textboxEmail = new TextBoxState("", "", false, true);
var textboxPhoneCode = new TextBoxState("", "", false, true);
var textboxPhonePrefix = new TextBoxState("", "", false, true);
var textboxPhoneSuffix = new TextBoxState("", "", false, true);

//

function Initialize() {

    //ID
    sectorID = new SectorState("[id$=ObligatoryFieldID]", "[id$=RightFieldID]");

    textboxName = new TextBoxState("[id$=TextBoxName]", "Campo obligatorio | Apellido | Empresa", true, true);
    textboxSurname = new TextBoxState("[id$=TextBoxSurname]", "", true, true);
    textboxCompany = new TextBoxState("[id$=TextBoxCompany]", "", true, true);

    sectorID.TextBoxArray[0] = textboxName;
    sectorID.TextBoxArray[1] = textboxSurname;
    sectorID.TextBoxArray[2] = textboxCompany;
    sectorID.IsValid = false;

    //Contact
    sectorContact = new SectorState("[id$=ObligatoryFieldContact", "[id$=RightFieldContact]");

    textboxEmail = new TextBoxState("[id$=TextBoxEmail]", "Campo obligatorio | Email válido | Teléfono", true, true);
    textboxPhoneCode = new TextBoxState("[id$=TextBoxPhoneCode]", "Código de area", true, true);
    textboxPhonePrefix = new TextBoxState("[id$=TextBoxPhonePrefix]", "Prefijo", true, true);
    textboxPhoneSuffix = new TextBoxState("[id$=TextBoxPhoneSuffix]", "Sufijo", true, true);

    sectorContact.TextBoxArray[0] = textboxEmail;
    sectorContact.TextBoxArray[1] = textboxPhoneCode;
    sectorContact.TextBoxArray[2] = textboxPhonePrefix;
    sectorContact.TextBoxArray[3] = textboxPhoneSuffix;
    sectorContact.IsValid = false;

}

function ClearForm() {
    $(".FormTextBox").val("");
}

function LoadControls() {
    $(".FormCheckFields").css("display", "none");
    $(".FormRightField").css("display", "none");
    if ($(".FormMessageSended").css("display") != "none") {
        $(".FormMessageSended").fadeOut(3000);
    }
}

function AssignEventHandlers() {
    Initialize();

    for (var i = 0; i <= 2; i++) {
        var textboxClientID = sectorID.TextBoxArray[i].ClientID;
        $(textboxClientID).keyup(function () { ValidSector(this, "ID"); });
        $(textboxClientID).change(function () { ValidSector(this, "ID"); });
    }

    for (var i = 0; i <= 3; i++) {
        var textboxClientContact = sectorContact.TextBoxArray[i].ClientID;
        $(textboxClientContact).focusout(function () {
            ValidWaterMark(this, "Contact");
            $(textboxClientContact).keyup(function () { ValidSector(this, "Contact"); });
            $(textboxClientContact).change(function () { ValidSector(this, "Contact"); });
        });
    }

    $(textboxName.ClientID).focusout(function () { ValidWaterMark(this, "ID"); });
    $(".FormTextBox").focusin(function () { WaterMark(this); });
}

function ValidSector(changedTextBox, sectorType) {

    switch (sectorType) {
        case "ID":
            var textTest = sectorID.TextBoxArray[1];
            alert(sectorID.TextBoxArray[1]["ClientID"]);
            if (!ValueIsNull($(changedTextBox).val())) {
                sectorID.IsValid = true;
            }
            else if (ValueIsNull($(changedTextBox).val())
            & (textboxName.IsWaterMarked || ValueIsNull($(textboxName.ClientID).val()))
            && ValueIsNull($(textboxSurname.ClientID).val())
            && ValueIsNull($(textboxCompany.ClientID).val())) {
                sectorID.IsValid = false;

            }

            if (sectorID.IsValid) {
                $(sectorID.ObligatoryFieldID).fadeOut(0);
                $(sectorID.RightFieldID).fadeIn(500);
                return true;
            }
            else {
                $(sectorID.RightFieldID).fadeOut(0);
                $(sectorID.ObligatoryFieldID).fadeIn(500);
                return false;
            }
            break;
        case "Contact":
            var validEmail = false;
            var validPhone = false;

            if (
            (
            ValueIsEmail($(textboxEmail.ClientID).val())
            && !ValueIsNull($(textboxEmail.ClientID).val())
            )
            ||
            ($(textboxEmail.ClientID).val() == textboxEmail.StringWaterMark)
            ) {
                alert("vWATA");
                validEmail = true;

            }
            else {
                alert("WATA");
                validEmail = false;
            }

            if (
            (
            textboxPhoneCode.IsWaterMarked
            && textboxPhonePrefix.IsWaterMarked
            && textboxPhoneSuffix.IsWaterMarked
            )
            ||
            (
            ValueIsPhoneNumeric($(textboxPhoneCode.ClientID).val())
            && ValueIsPhoneNumeric($(textboxPhonePrefix.ClientID).val())
            && ValueIsPhoneNumeric($(textboxPhoneSuffix.ClientID).val())
            )
            ) {
                validPhone = true;
            }
            else {
                alert("WATA");
                validPhone = false;
            }

            if (validPhone) {
                sectorContact.IsValid = true;
            }
            else {
                sectorContact.IsValid = false;
            }


            if (sectorContact.IsValid) {
                $(sectorContact.ObligatoryFieldID).fadeOut(0);
                $(sectorContact.RightFieldID).fadeIn(500);
                return true;
            }
            else {
                $(sectorContact.RightFieldID).fadeOut(0);
                $(sectorContact.ObligatoryFieldID).fadeIn(500);
                return false;
            }
            break;
        case "Message":
            break;
    }
}

function ValidWaterMark(changedTextbox, sectorType) {
    switch (sectorType) {
        case "ID":
            if (ValueIsNull($(textboxName.ClientID).val())) {
                $(textboxName.ClientID).val(textboxName.StringWaterMark);
                textboxName.IsWaterMarked = true;
            }
            else {
                textboxName.IsWaterMarked = false;
            }
            break;
        case "Contact":
            if (
                ValueIsNull($(textboxEmail.ClientID).val())
                ||
                (
                !ValueIsNull($(textboxEmail.ClientID).val())
                && ($(textboxEmail.ClientID).val() == textboxEmail.StringWaterMark)
                )
                ) {
                $(textboxEmail.ClientID).val(textboxEmail.StringWaterMark);
                textboxEmail.IsWaterMarked = true;
            }
            else {
                textboxEmail.IsWaterMarked = false;
            }
            if (ValueIsNull($(textboxPhoneCode.ClientID).val())) {
                $(textboxPhoneCode.ClientID).val(textboxPhoneCode.StringWaterMark);
                textboxPhoneCode.IsWaterMarked = true;
            }
            else {
                textboxPhoneCode.IsWaterMarked = false;
            }
            if (ValueIsNull($(textboxPhonePrefix.ClientID).val())) {
                $(textboxPhonePrefix.ClientID).val(textboxPhonePrefix.StringWaterMark);
                textboxPhonePrefix.IsWaterMarked = true;
            }
            else {
                textboxPhonePrefix.IsWaterMarked = false;
            }
            if (ValueIsNull($(textboxPhoneSuffix.ClientID).val())) {
                $(textboxPhoneSuffix.ClientID).val(textboxPhoneSuffix.StringWaterMark);
                textboxPhoneSuffix.IsWaterMarked = true;
            }
            else {
                textboxPhoneSuffix.IsWaterMarked = false;
            }
            break;
        case "Message":
            break;
    }
}

function WaterMark(TextBox) {
    if (
    $(TextBox).val() == "Campo obligatorio"
    || $(TextBox).val() == "Campo obligatorio | Apellido | Empresa"
    || $(TextBox).val() == "Campo obligatorio | Email válido | Teléfono"
    || $(TextBox).val() == "Código de area"
    || $(TextBox).val() == "Prefijo"
    || $(TextBox).val() == "Sufijo"
    || $(TextBox).val() == "(Sin asunto)"
    ) {
        $(TextBox).val("");
    }
}